package com.eop.managedBean;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.form.impl.UISimpleHtmlEditor;
import org.operamasks.faces.component.form.impl.UITextField;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.security.SecurityUserHolder;
import com.eop.entity.Schnario;
import com.eop.service.ISchnarioService;

@ManagedBean(name = "eop_listSchnarioBean", scope = ManagedBeanScope.REQUEST)
public class ListSchnarioBean {
	private Log logger = LogFactory.getLog(ListSchnarioBean.class);
	private ISchnarioService schnarioService = SpringBeanHolder
			.getBean("schnarioService");
	@Inject
	private FacesContext context;

	@Bind(id = "schnarioName", attribute = "value")
	@SaveState
	private String schnarioName_value;

	@Bind(id = "schnarioGrid", attribute = "binding")
	private UIDataGrid schnarioGrid_binding;

	@Bind(id = "reContent", attribute = "value")
	private String reContent;
	@Bind(id = "schId", attribute = "value")
	private String schId;
	@Bind(id = "reContent", attribute = "binding")
	private UISimpleHtmlEditor reContent_binding;
	@Bind(id = "schId", attribute = "binding")
	protected UITextField schId_binding;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			schnarioName_value = null;
		}
	}

	@Action(id = "search")
	public void search() {
		schnarioGrid_binding.reload();
	}

	@Action(id = "reset")
	public void reset() {
		schnarioName_value = null;
		schnarioGrid_binding.reload();
	}

	@DataModel(id = "schnarioGrid")
	private List<Schnario> getSchnarioGrid() {
		// 搜索条件写入搜索
		List<Schnario> chList = new ArrayList<Schnario>();
		if (schnarioName_value != null && !"".equals(schnarioName_value)) {
			chList = schnarioService.getSchnarioByName(schnarioName_value);
		} else {
			chList = schnarioService.getAllEffectSchnario();
		}
		return chList;
	}

	@Action(id = "deleteSchnario")
	public void deleteSchnario() {
		Object[] obj = schnarioGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		Schnario sch = (Schnario) obj[0];
		// 逻辑删除
		// schnarioService.deleteSchnario(sch);
		sch.setDelFlag(true);
		schnarioService.saveSchnario(sch);
		schnarioGrid_binding.reload();

	}

	@Action(id = "eidtSchnario")
	public void eidtSchnario() {
		Object[] obj = schnarioGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		Schnario sch = (Schnario) obj[0];

		if (sch.isPublishState()) {
			Browser.execClientScript("window.alert('该预案已经发布，不能再被编辑 ！');");
			return;
		}
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/eop/editSchnario.faces?id=" + sch.getId();
		String script = "addCenterLayoutTab('editSchnarioTab','编辑预案','" + url
				+ "')";
		Browser.execClientScript("window.top", script);
	}

	@Action(id = "publish")
	public void publish() {
		String id = context.getExternalContext().getRequestParameterMap().get(
				"id");
		Schnario sch = schnarioService.getSchById(id);
		sch.setPublishState(true);
		sch.setPublishDate(new Date());
		sch.setPublishDep(SecurityUserHolder.getCurrentDepartmentId());
		schnarioService.saveSchnario(sch);
		schnarioGrid_binding.reload();

	}

	@Action(id = "showRepaire")
	public void showRepaire() {
		String id = context.getExternalContext().getRequestParameterMap().get(
				"id");
		Schnario sch = schnarioService.getSchById(id);
		schId_binding.setValue(id);
		schId_binding.repaint();
		if (sch.getContent() != null) {
			byte[] msgContent = sch.getContent();
			if (msgContent.length > 0) {
				String content;
				try {
					content = new String(msgContent, "gb2312");
					reContent_binding.setValue(content);
					reContent_binding.repaint();
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}

			}
		} else {
			reContent_binding.setValue(null);
			reContent_binding.repaint();
		}
		Browser.execClientScript("repaireWinJsvar.show();");
	}

	@Action(id = "saveRepair")
	public void saveRepair() {
		Schnario sch = schnarioService.getSchById(schId);
		byte[] byBuffer = new byte[1024];
		try {
			byBuffer = reContent.getBytes("gb2312");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		sch.setContent(byBuffer);
		schnarioService.saveSchnario(sch);
		Browser.execClientScript("repaireWinJsvar.hide();");

	}
}
